/********************************************************
  *      Real Time Clock DS1307 Library for 18F4620     *
  *                  (for PIC IC.)                      *
  *                 by Deniz ELMASLI                    *
  * Version 2.0                                         *
  * Creation Date: 28.12.2008   -   10:12 Friday        *
  * Revision Date: 08.06.2008   -   03:42 Saturday      *
  * Ide(s) : Code::Blocks v1.0 (C++ developement)       *
  *          MikroC V7.0                                *
  * Target Platform: PIC                                *
  * MCU : 18F4620                                       *
  * Contact: elmasli@gmail.com                          *
  * Web    : www.dronology.com                          *
  *                                                     *
  *   This code in intended to work with PIC IC. This   *
  * source code contains functions to drive DS1307 real *
  * time clock  . I2C Functions used in ver2.0          *
  *******************************************************
*/

#include "DS1307.h"
void DS1307_Init(){

    I2C1_Init(100000);
    I2C1_Start();
    I2C1_Wr( 0xD0);           // Slave address of DS1307
    I2C1_Wr( 0x00);           // Start from word at Address REG0
    //I2C1_Wr (0x00);           // Write 0 to REG0
    I2C1_Stop();
}
unsigned short DS1307_BCD2Dec(unsigned short n){
    return(((( n & 0x70)>>4) * 10)+ (n & 0x0F));
}
unsigned short DS1307_Dec2BCD(unsigned short n){

    return (((n/10)<<4)+(n%10));

}
                              
void DS1307_GetDateTime(RTC_Time *p)
    {
        I2C1_Start();
        I2C1_Wr(0xD0);
        I2C1_Wr(0x00);
        I2C1_Repeated_Start();
        I2C1_Wr(0xD1);
    
        p->sec      = DS1307_BCD2Dec(I2C1_Rd(1));
        p->min      = DS1307_BCD2Dec(I2C1_Rd(1));
        p->hour     = DS1307_BCD2Dec(I2C1_Rd(1));
        p->week_day = DS1307_BCD2Dec(I2C1_Rd(1));
        p->day      = DS1307_BCD2Dec(I2C1_Rd(1));
        p->month    = DS1307_BCD2Dec(I2C1_Rd(1));
        p->year     = DS1307_BCD2Dec(I2C1_Rd(1));
        I2C1_Stop() ;

    }
    
void DS1307_SetDateTime(RTC_Time *p)
    {

        I2C1_Start();
        I2C1_Wr(0xD0);
        I2C1_Wr(0x00);
        I2C1_Wr(DS1307_Dec2BCD(p->sec));             //REGO
        I2C1_Wr(DS1307_Dec2BCD(p->min));             //REG1
        I2C1_Wr(DS1307_Dec2BCD(p->hour));            //REG2
        I2C1_Wr(DS1307_Dec2BCD(p->week_day));        //REG3
        I2C1_Wr(DS1307_Dec2BCD(p->day));             //REG4
        I2C1_Wr(DS1307_Dec2BCD(p->month));           //REG5
        I2C1_Wr(DS1307_Dec2BCD(p->year));            //REG6
        I2C1_Wr(0x80);
        I2C1_Stop();
    
    }

void DS1307_LCD_Display_Time(RTC_Time *p)
    {

        Lcd_Chr(1, 6, (p->day / 10)   + 48);    // Print tens digit of day variable
        Lcd_Chr(1, 7, (p->day % 10)   + 48);    // Print oness digit of day variable
        Lcd_Chr(1, 9, (p->month / 10) + 48);
        Lcd_Chr(1,10, (p->month % 10) + 48);
        Lcd_Chr(1,14, (p->year / 10)   + 48);    // Print year variable  (start from year 2010)
        Lcd_Chr(1,15, (p->year %10)   + 48);


        Lcd_Chr(2, 6, (p->hour / 10)   + 48);
        Lcd_Chr(2, 7, (p->hour % 10)   + 48);
        Lcd_Chr(2, 9, (p->min / 10) + 48);
        Lcd_Chr(2,10, (p->min % 10) + 48);
        Lcd_Chr(2,12, (p->sec / 10) + 48);
        Lcd_Chr(2,13, (p->sec % 10) + 48);


    }
/**************************End of file*****************************************/